package com.xmtlxx.wms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.wms.domain.bo.LocatorBo;
import com.xmtlxx.wms.domain.vo.CabinetGroupVo;
import com.xmtlxx.wms.domain.vo.CabinetVo;
import com.xmtlxx.wms.domain.vo.LocatorVo;
import com.xmtlxx.wms.domain.vo.StoreAreaVo;
import com.xmtlxx.wms.service.ICabinetGroupService;
import com.xmtlxx.wms.service.ICabinetService;
import com.xmtlxx.wms.service.ILocatorService;
import com.xmtlxx.wms.service.IStoreAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 库位Controller
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Validated
@Api(value = "库位控制器", tags = {"库位管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/wms/locator")
public class LocatorController extends BaseController {

    private final ILocatorService iLocatorService;

    private final ICabinetGroupService iCabinetGroupService;

    private final IStoreAreaService iStoreAreaService;

    private final ICabinetService iCabinetService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<LocatorVo> initBean(@Validated(QueryGroup.class) @RequestBody LocatorVo vo){
        //货柜跳转到库位
        if(StringUtils.isNotEmpty(vo.getCabinetGroupId())){
           CabinetGroupVo cabinetGroupVo =  iCabinetGroupService.queryById(vo.getCabinetGroupId());
           vo.setStoreId(cabinetGroupVo.getStoreId());
           vo.setAreaId(cabinetGroupVo.getAreaId());
           vo.setCabinetGroupId(cabinetGroupVo.getId());
           vo.setRegion(cabinetGroupVo.getRegion());
           if(cabinetGroupVo.getType().equals("0")){
               vo.setIsShow(true);
           }else {
               vo.setIsShow(false);
           }
           vo.setRegion(cabinetGroupVo.getRegion());
        }else {
            //柜列跳转到库位
            if(StringUtils.isNotEmpty(vo.getCabinetId())){
                CabinetVo cabinetVo =  iCabinetService.queryById(vo.getCabinetId());
                CabinetGroupVo cabinetGroupVo =  iCabinetGroupService.queryById(cabinetVo.getGroupId());
                vo.setStoreId(cabinetVo.getStoreId());
                vo.setAreaId(cabinetVo.getAreaId());
                vo.setCabinetGroupId(cabinetVo.getGroupId());
                if(cabinetVo.getType().equals("0")){
                    vo.setIsShow(true);
                }else {
                    vo.setIsShow(false);
                }
                vo.setRegion(cabinetGroupVo.getRegion());
            }
        }

        vo.setStatus("0");
        vo.setShapeList(new ArrayList<>());
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询库位列表
     */
    @ApiOperation("查询库位列表")
//    @SaCheckPermission("wms:locator:list")
    @GetMapping("/list")
    public TableDataInfo<LocatorVo> list(@Validated(QueryGroup.class) LocatorBo bo, PageQuery pageQuery) {
        if(StringUtils.isEmpty(bo.getDelFlag())){
            bo.setDelFlag("0");
        }
        return iLocatorService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询库位列表
     */
    @ApiOperation("查询库位列表")
    @GetMapping("/locatores")
    public R<List<LocatorVo>> locatores(@Validated(QueryGroup.class) LocatorBo bo) {
        if(StringUtils.isEmpty(bo.getDelFlag())){
            bo.setDelFlag("0");
        }
        return R.ok(iLocatorService.queryList(bo));
    }

    /**
     * 导出库位列表
     */
    @ApiOperation("导出库位列表")
    @SaCheckPermission("wms:locator:export")
    @Log(title = "库位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated LocatorBo bo, HttpServletResponse response) {
        if(StringUtils.isEmpty(bo.getDelFlag())){
            bo.setDelFlag("0");
        }
        List<LocatorVo> list = iLocatorService.queryList(bo);
        ExcelUtil.exportExcel(list, "库位", LocatorVo.class, response);
    }

    /**
     * 获取库位详细信息
     */
    @ApiOperation("获取库位详细信息")
//    @SaCheckPermission("wms:locator:query")
    @GetMapping("/{id}")
    public R<LocatorVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") String id) {
        LocatorVo vo = iLocatorService.queryById(id);
        if(StringUtils.isNotEmpty(vo.getShape())){
            vo.setShapeList(Arrays.asList(vo.getShape().split(",")));
        }else {
            vo.setShapeList(new ArrayList<>());
        }

        StoreAreaVo areaVo = iStoreAreaService.queryById(vo.getAreaId());

        if(areaVo != null){
            vo.setAreaNo(areaVo.getAno());
        }

        CabinetVo cabinetVo =  iCabinetService.queryById(vo.getCabinetId());
        if(cabinetVo != null){
            if(cabinetVo.getType().equals("0")){
                vo.setIsShow(true);
            }else {
                vo.setIsShow(false);
            }
        }

        return R.ok(vo);
    }

    /**
     * 新增库位
     */
    @ApiOperation("新增库位")
    @SaCheckPermission("wms:locator:add")
    @Log(title = "库位", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody LocatorVo vo) {
        LocatorBo bo = new LocatorBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iLocatorService.insertByBo(bo) ? 1 : 0);
    }

    @ApiOperation("批量新增库位")
    @SaCheckPermission("wms:locator:add")
    @Log(title = "库位", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/addBatchLocator")
    public R<Void> addBatchLocator(@Validated(AddGroup.class) @RequestBody LocatorVo vo) {
        Boolean flag = iLocatorService.insertBatchByVo(vo);
        iLocatorService.countLocators(vo.getCabinetGroupId());
        return toAjax( flag? 1 : 0);
    }

    @ApiOperation("批量新增格口")
    @SaCheckPermission("wms:locator:add")
    @Log(title = "批量新增格口", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/addBatchLocators")
    public R<Void> addBatchLocators(@Validated(AddGroup.class) @RequestBody LocatorVo vo) {
        String cabinetGroupId = iLocatorService.insertBatchLocatorByVo(vo);
        iLocatorService.countLocators(cabinetGroupId);
        return toAjax(1);
    }

    @ApiOperation("批量分配货架")
    @SaCheckPermission("wms:locator:edit")
    @Log(title = "批量分配货架", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/planBatchLocators")
    public R<Void> planBatchLocators(@Validated(AddGroup.class) @RequestBody LocatorVo vo) {
        return toAjax(iLocatorService.planBatchLocatorByVo(vo));
    }


    /**
     * 修改库位
     */
    @ApiOperation("修改库位")
    @SaCheckPermission("wms:locator:edit")
    @Log(title = "库位", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody LocatorVo vo) {
        LocatorBo bo = new LocatorBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iLocatorService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除库位
     */
    @ApiOperation("删除库位")
    @SaCheckPermission("wms:locator:remove")
    @Log(title = "库位", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iLocatorService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
